﻿@page "/Centre/Resource"
@using iOSClub.Data
@using iOSClub.Data.DataModels
@using iOSClub.WebSite.Models
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<iOSContext> DbFactory
@inject NavigationManager Nav
@inject MessageService MessageService

<PageTitle>社团资源 -iStaff</PageTitle>

<PageView Style="min-height: 100%"
          FullScreen="true"
          OnDelete="@(() => Nav.NavigateTo("/Centre"))"
          Title="社团资源">
    <Extra>
        @if (Member.IsAdmin())
        {
            <Button Type="@ButtonType.Primary" OnClick="@(() => _visible = true)">
                添加资源
            </Button>
        }
    </Extra>
    <Context>
        @if (Resources.Count == 0)
        {
            <Empty Image="https://gw.alipayobjects.com/zos/antfincdn/ZHrcdLPrvN/empty.svg" Style="padding: 20px">
                <DescriptionTemplate>
                    <span>社团现在还没有任何资源</span>
                </DescriptionTemplate>
            </Empty>
        }
        else
        {
            <AntList Grid="grid" DataSource="@Resources">
                <ListItem>
                    <Card Hoverable Style="border-radius: 10px;" BodyStyle="margin: 5px auto">
                        <Flex Vertical Justify="space-between" Style="height: 200px" Align="flex-start">
                            <div>
                                <Title Level="2">@context.Name</Title>
                                <Paragraph>@context.Description</Paragraph>
                                <div>
                                    @foreach (var s in context.Tag?.Split(",", StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>())
                                    {
                                        <Tag Color="blue-inverse">@s</Tag>
                                    }
                                </div>
                            </div>

                            @if (Member.IsAdmin())
                            {
                                <Space>
                                    <SpaceItem>
                                        <Button Type="@ButtonType.Primary" OnClick="@(() => Update(context))">更改
                                        </Button>
                                    </SpaceItem>
                                    <SpaceItem>
                                        <Button Danger OnClick="@(() => Delete(context))">删除</Button>
                                    </SpaceItem>
                                </Space>
                            }
                        </Flex>
                    </Card>
                </ListItem>
            </AntList>
        }
    </Context>
</PageView>

<Modal @bind-Visible="@_visible" Footer="@null">
    <Form Model="FormData" OnFinish="Create" ValidateMode="FormValidateMode.Rules">
        <FormItem Label="资源名称" Rules="@SignRecord.ResourceNameRules">
            <Input @bind-Value="@context.Name"/>
        </FormItem>
        <FormItem Label="资源描述" Rules="@SignRecord.ResourceDescriptionRules">
            <TextArea @bind-Value="@context.Description"></TextArea>
        </FormItem>
        <FormItem Label="资源标签">
            <Input @bind-Value="@context.Tag"/>
        </FormItem>
        <FormItem Style="text-align: center">
            <Button Type="@ButtonType.Primary" HtmlType="submit">
                添加
            </Button>
        </FormItem>
    </Form>
</Modal>

@code
{
    private bool _visible;
    private ResourceModel FormData { get; set; } = new();

    private async Task Create()
    {
        await using var context = await DbFactory.CreateDbContextAsync();
        if (!string.IsNullOrEmpty(FormData.Id))
        {
            var resource = await context.Resources.FirstOrDefaultAsync(x => x.Id == FormData.Id);
            if (resource != null)
            {
                resource.Name = FormData.Name;
                resource.Description = FormData.Description;
                resource.Tag = FormData.Tag;
                await context.SaveChangesAsync();
                await MessageService.Success("修改成功");
            }
        }
        else
        {
            FormData.Id = DataTool.StringToHash(FormData.ToString());
            await context.Resources.AddAsync(FormData);
            await context.SaveChangesAsync();
            Resources.Add(FormData);
            await MessageService.Success("添加成功");
        }

        _visible = false;
        FormData = new ResourceModel();
    }

    private async Task Delete(ResourceModel model)
    {
        await using var context = await DbFactory.CreateDbContextAsync();
        context.Resources.Remove(model);
        await context.SaveChangesAsync();
        Resources.Remove(model);
        await MessageService.Success("删除成功");
    }
}

@code {

    private static ListGridType grid => new()
    {
        Gutter = 16,
        Xs = 1,
        Sm = 2,
        Md = 3,
        Lg = 3,
        Xl = 3,
        Xxl = 3,
    };


    private List<ResourceModel> Resources { get; set; } = [];
    [CascadingParameter] private MemberModel Member { get; set; } = new();

    protected override async Task OnInitializedAsync()
    {
        if (Member.Identity == "Member")
        {
            Nav.NavigateTo("/Centre");
            return;
        }

        await using var context = await DbFactory.CreateDbContextAsync();

        Resources = await context.Resources.ToListAsync();

        await base.OnInitializedAsync();
    }

    private void Update(ResourceModel context)
    {
        _visible = true;
        FormData = context;
    }

}